package com.yd.app.mapper.provider;

import com.yd.domain.searchParam.ShareParam;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author zy
 * @create 2018-03-09 17:57
 * @desc 分享信息
 **/
public class ShareProvider extends BaseProvider {
    /**
     * @Author: zy
     * @Desc: 分页查询数据
     * @Date:  2018/3/9
     * @Param
     */
    public String searchShareList(Map map){
        ShareParam shareParam= (ShareParam) map.get("shareParam");
        StringBuilder sb=new StringBuilder("select g.number,g.perWeight,g.price,g.title as goodtitle,n.goodId,n.id,n.`status`,n.title,n.des,n.publishTime,n.publishUser,s.realName as publishUserName,dic.title as statusName from business_share n " +
                "LEFT JOIN sys_dic dic on dic.`code`=n.`status` AND dic.type='shareStatus' " +
                "LEFT JOIN sys_user s on s.id=n.publishUser " +
                "LEFT JOIN business_goods g on g.id =n.goodId " +
                "WHERE 1=1");
        sb.append(createSearchWhereSql(shareParam));
        sb.append("  limit #{startRow},#{pageSize}");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:查询数据条数
     * @Date:  2018/3/7
     */
    public String searchShareCount(Map map){
        ShareParam shareParam= (ShareParam) map.get("shareParam");
        StringBuilder sb=new StringBuilder("SELECT COUNT(1) from business_share n  " +
                "  where 1=1 ");
        sb.append(createSearchWhereSql(shareParam));
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:查询条件
     * @Date:  2018/3/7
     */
    private String createSearchWhereSql(ShareParam shareParam){
        StringBuilder sb=new StringBuilder();
        if(!StringUtils.isEmpty(shareParam.getStartTime())){
            sb.append(" and n.publishTime >=#{shareParam.getStartTime}  ");
        }
        if(!StringUtils.isEmpty(shareParam.getEndTime())){
            sb.append(" and n.publishTime <=#{shareParam.getEndTime}  ");
        }
        if(!StringUtils.isEmpty(shareParam.getTitle())){
            sb.append(" and n.title like  '%"+shareParam.getTitle()+"%'  ");
        }
        if(!StringUtils.isEmpty(shareParam.getStatus())){
            sb.append(" and n.`status` = '"+shareParam.getStatus()+"'  ");
        }

        return sb.toString();
    }
}
